package com.example.pathprotectsystem.controller;

import com.example.pathprotectsystem.pojo.*;
import com.example.pathprotectsystem.service.BindService;
import com.example.pathprotectsystem.service.PathService;
import com.example.pathprotectsystem.service.RecordService;
import com.example.pathprotectsystem.service.UserService;
import com.example.pathprotectsystem.utils.DateTimeChangeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class BindController {
    @Autowired
    private BindService bindService;
    @Autowired
    private PathService pathService;
    @Autowired
    private UserService userService;
    @Autowired
    private RecordService recordService;
    @GetMapping("/getpaths")
    public Result<List<String>> getpaths(){
        List<String> p = pathService.getpaths();
        return Result.success(p);
    }
    @GetMapping("/getpersons")
    public Result<List<String>> getpersons(){
        List<String> p = userService.getpersons();
        return Result.success(p);
    }
    @PostMapping("/adddaychecktask")
    public Result adddaychecktask(@RequestBody TaskModel taskModel){
        int userid = userService.findUserIdByUserName(taskModel.getUserName());
        int pathid = pathService.findPathIdByPathName(taskModel.getPathName());
        String taskType = "日常巡检";
        String taskName = "日常检查";
        String taskTime = DateTimeChangeUtil.change(taskModel.getTaskTime());
        String status = "未填写";
        bindService.addtask(taskType,taskName,taskTime,status,userid,pathid);
        return Result.success();
    }

    @PostMapping("/addregularchecktask")
    public Result addregularchecktask(@RequestBody TaskModel taskModel){
        int userid = userService.findUserIdByUserName(taskModel.getUserName());
        int pathid = pathService.findPathIdByPathName(taskModel.getPathName());
        String taskType = "定期检查";
        String taskName1 = "路面行驶质量检查";
        String taskName2 = "路面损坏状况检查";
        String taskTime = DateTimeChangeUtil.change(taskModel.getTaskTime());
        String status = "未填写";
        bindService.addtask(taskType,taskName1,taskTime,status,userid,pathid);
        bindService.addtask(taskType,taskName2,taskTime,status,userid,pathid);
        return Result.success();
    }
    @PostMapping ("/tasklist")
    public Result<PageBean<BindModel>> tasklist(@RequestBody AskList askList){
        PageBean<BindModel> p = bindService.tasklist(askList);
        return Result.success(p);
    }
    @PostMapping("/taskupdate")
    public Result taskupdate(@RequestBody TaskModel taskModel){
        System.out.println(taskModel.getTaskTime());
        int userid = userService.findUserIdByUserName(taskModel.getUserName());
        int pathid = pathService.findPathIdByPathName(taskModel.getPathName());
        String str = taskModel.getTaskTime();
        if(str.charAt(str.length()-1)=='Z') str = DateTimeChangeUtil.change(taskModel.getTaskTime());
        System.out.println(str);
        int id = taskModel.getId();
        bindService.taskupdate(userid,pathid,str,id);
        return Result.success();
    }
    @DeleteMapping("/taskdelete")
    public Result taskdelete(int id){
        int sum = recordService.findTaskIdFromf1(id);
        sum+=recordService.findTaskIdFromf2(id);
        sum+=recordService.findTaskIdFromf3(id);
        sum+=recordService.findTaskIdFromf4(id);
        System.out.println("sum = "+sum);
        if(sum==0){
            bindService.taskdelete(id);
            return Result.success();
        }
        return Result.error("有记录表存在该巡查或检查任务信息，无法删除！");
    }
    @PostMapping("/AIadddaycheck")
    public Result AIadddaycheckservice(@RequestBody TaskModel taskModel){
        int pathid = pathService.findPathIdByPathName(taskModel.getPathName());
        String taskType = "日常巡检";
        String taskName = "日常检查";
        String status = "未填写";
        bindService.AIaddtask(pathid,taskType,taskName,status);
        return Result.success();
    }
    @PostMapping("/AIaddregularchecktask")
    public Result AIaddregularchecktask(@RequestBody TaskModel taskModel){
        int pathid = pathService.findPathIdByPathName(taskModel.getPathName());
        String taskType = "定期检查";
        String taskName1 = "路面行驶质量检查";
        String taskName2 = "路面损坏状况检查";
        String status = "未填写";
        bindService.AIaddtask(pathid,taskType,taskName1,status);
        bindService.AIaddtask(pathid,taskType,taskName2,status);
        return Result.success();
    }
}
